package com.ww.word.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.ww.core.util.DeviceUtil;
import com.ww.core.util.ImageUtil;
import com.ww.core.util.MusicUtil;
import com.ww.core.util.StringUtils;
import com.ww.word.R;
import com.ww.word.db.StudyDbHelper;
import com.ww.word.entity.Word;
import com.ww.word.ui.DetailActivity;
import com.ww.word.ui.GroupActivity;
import com.ww.word.ui.TuyaActivity;

/**
 * @since 
 * @version 2013-12-12
 * @author wangwei
 */
public class WordAdapter {
	private View parent;
	private TextView hanzi;
	private LinearLayout group;
	private Button detail;
	private Button goback;
	private Button next, practise;
	private Button store;
	private Activity context;
	private Word word;
	private LinearLayout pinyin;

	public WordAdapter(final Activity context, final Word word) {
		this.context = context;
		this.word = word;
		parent = LayoutInflater.from(context).inflate(R.layout.activity_study_item, null);
		pinyin = (LinearLayout) parent.findViewById(R.id.pinyin);
		hanzi = (TextView) parent.findViewById(R.id.word);
		group = (LinearLayout) parent.findViewById(R.id.group);
		detail = (Button) parent.findViewById(R.id.detail);
		goback = (Button) parent.findViewById(R.id.goback);
		next = (Button) parent.findViewById(R.id.next);
		store = (Button) parent.findViewById(R.id.store);
		practise = (Button) parent.findViewById(R.id.practise);
	}

	public View getReviewView() {
		baseView();
		next.setVisibility(View.GONE);
		goback.setVisibility(View.GONE);
		return parent;
	}

	public View getStudyView(final ViewFlipper page, int index) {
		baseView();
		next.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				StudyDbHelper w = new StudyDbHelper(context);
				w.saveStudy(word.getId(), word.getName(), word.getSimple_group(), word.getPinyin());
				page.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_in));
				page.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_out));
				page.showNext();
			}
		});

		if (index == 0) {
			goback.setVisibility(View.GONE);
		}

		goback.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				page.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
				page.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_out));
				page.showPrevious();
			}
		});

		return parent;
	}

	private void baseView() {
		hanzi.setText(word.getName());
		pinyinLayout(word.getPinyin());
		groupLayout(word.getSimple_group());
		StudyDbHelper w = new StudyDbHelper(context);
		if (w.checkStore(word.getId())) {
			setImage(true);
		} else {
			setImage(false);
		}

		//		ImageView img3 = (ImageView) parent.findViewById(R.id.img);
		//		try {
		//			Bitmap bit = BitmapFactory.decodeStream(context.getAssets().open("w" + word.getId() + ".jpg"));
		//			img3.setImageBitmap(bit);
		//		} catch (IOException e) {
		//			e.printStackTrace();
		//		}

		detail.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, DetailActivity.class);
				intent.putExtra("idWord", word.getId());
				intent.putExtra("nameWord", word.getName());
				context.startActivity(intent);
			}
		});

		practise.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, TuyaActivity.class);
				intent.putExtra("group", word.getSimple_group());
				intent.putExtra("name", word.getName());
				context.startActivity(intent);
			}
		});

		store.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				StudyDbHelper w = new StudyDbHelper(context);
				String str = w.updateStore(word.getId(), word.getName(), word.getSimple_group(), word.getPinyin());
				if (str.contains("成功")) {
					if (str.contains("取消")) {
						setImage(false);
						Toast.makeText(context, "取消收藏成功", Toast.LENGTH_SHORT).show();
					} else {
						setImage(true);
						Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
				}
				sendBroadcast();
			}
		});
	}
	
	private void sendBroadcast(){
		Intent i = new Intent();
		i.setAction("store");
		context.sendBroadcast(i);
	}

	/**
	 * 拼装拼音.
	 * @param str 拼音
	 */
	private void pinyinLayout(String str) {
		if (StringUtils.isNotEmpty(str)) {
			String[] ddd = str.split(",");
			for (int i = 0; i < ddd.length; i++) {
				LayoutInflater inflater = LayoutInflater.from(context);
				View view = inflater.inflate(R.layout.activity_study_item_pinyin, null);
				ImageView image = (ImageView) view.findViewById(R.id.pinyin_img);// 取得实例
				TextView title = (TextView) view.findViewById(R.id.pinyin_txt);// 取得实例

				final String ss[] = ddd[i].split(" ");
				title.setText(ss[0].replace("[", "").replace("]", ""));
				image.setImageResource(R.drawable.voip_speaker);
				image.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (DeviceUtil.checkNet(context)) {
							MusicUtil m = new MusicUtil(context, ss[1]);
							m.start();
						}
					}
				});
				pinyin.addView(view);
			}
		}
	}

	/**
	 * 拼装词组.
	 * @param str 简单词组
	 */
	private void groupLayout(String str) {
		if (str != null) {
			String[] ddd = str.split(",");
			for (int i = 0; i < ddd.length; i++) {
				TextView title = new TextView(context);// 取得实例
				title.setPadding(0, 10, 10, 5);
				title.setTextSize(20);
				title.setTextColor(Color.BLUE);
				title.setText(ddd[i]);

				final String ss[] = ddd[i].split(" ");
				title.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context, GroupActivity.class);
						intent.putExtra("name", ss[0]);
						context.startActivity(intent);
					}
				});
				group.addView(title);
			}
		}
	}

	private void setImage(boolean bool) {
		if (bool) {
			store.setText("已收藏");
			Drawable db = ImageUtil.getDrawable(context, R.drawable.icon_tab_store_yes);
			db.setBounds(0, 0, db.getMinimumWidth(), db.getMinimumHeight());
			store.setCompoundDrawables(db, null, null, null);
		} else {
			store.setText("未收藏");
			Drawable db = ImageUtil.getDrawable(context, R.drawable.icon_tab_store_no);
			db.setBounds(0, 0, db.getMinimumWidth(), db.getMinimumHeight());
			store.setCompoundDrawables(db, null, null, null);
		}
	}

}
